from PyQt6.QtWidgets import QApplication, QGridLayout
from qfluentwidgets import CardWidget, BodyLabel, StrongBodyLabel, SubtitleLabel, TitleLabel, IconWidget, FluentIcon
from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QIcon, QColor
from datetime import datetime


class OddsCard(CardWidget):
    """卡片"""
    # 定义一个信号，传递类型为 自定义对象
    signal_odds = pyqtSignal(object)
    
    def __init__(self, game, parent=None):
        super().__init__(parent)
        # 添加图标
        self.__up_down_icon = IconWidget(FluentIcon.AIRPLANE)
        self.__up_down_icon.setFixedSize(20, 20)
        self.__up_down_icon.setIcon(QIcon(f"./ui/images/{game['icon']}.png"))
        
        # 开赛时间
        original_str = game['kick_off_time']
        # 解析字符串为 datetime 对象
        dt = datetime.strptime(original_str, "%Y-%m-%d %H:%M:%S")
        self.__kick_off_time = StrongBodyLabel(dt.strftime("%Y-%m-%d %H:%M"))
        self.__kick_off_time.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.__kick_off_time.setStyleSheet("padding-left: 10px; background-color: rgb(212,234,238); border-radius: 1px;")
        # 联赛
        self.__league = StrongBodyLabel(game['league'])
        self.__league.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.__league.setStyleSheet("background-color: rgb(212,234,238); border-radius: 1px;")
        
        # 赛事
        self.__team = StrongBodyLabel(game['team'])
        self.__team.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        # 对冲信息
        self.__infos = game['infos']
        
        # 利润百分百
        self.__profit = TitleLabel(game['profit'])
        self.__profit.setStyleSheet("padding-right: 10px; color: rgb(205, 28, 6")
        # self.__profit.setAlignment(Qt.AlignmentFlag.AlignCenter)
        water_profit_color = '#D81E06' if game['icon'] == 'up' else '#1296DB'
        self.__profit.lightColor = QColor(water_profit_color)
        
        self.setup_ui()

    def setup_ui(self):
        """设置ui"""
        # 创建QGridLayout删格布局
        grid_layout = QGridLayout()
        grid_layout.setContentsMargins(0, 0, 0, 0)
        grid_layout.setSpacing(8)  # 设置卡片内部控件的间距
        sub_grid_layout = QGridLayout()
        sub_grid_layout.setContentsMargins(0, 0, 0, 0)
        sub_grid_layout.setSpacing(0)  # 设置卡片内部控件的间距
        
        grid_layout.addWidget(self.__kick_off_time, 0, 0, 1, 2) # 开赛时间
        grid_layout.addWidget(self.__league, 0, 2, 1, 2) # 联赛
        
        grid_layout.addWidget(self.__team, 1, 0, 1, 2) # 赛事
        grid_layout.addWidget(self.__up_down_icon, 1, 2, 1, 1, Qt.AlignmentFlag.AlignRight)  # 图标
        grid_layout.addWidget(self.__profit, 1, 3)  # 利润百分百
        
        for index, info in enumerate(self.__infos):
            row = index+2
            type = BodyLabel(info['type'])
            type.setStyleSheet("padding-left: 10px;")
            ratio = BodyLabel(info['ratio'])
            ratio.setAlignment(Qt.AlignmentFlag.AlignLeft)
            odds = BodyLabel(info['odds'])
            odds.setAlignment(Qt.AlignmentFlag.AlignRight)
            source = BodyLabel(info['source'])
            source.setAlignment(Qt.AlignmentFlag.AlignCenter)
            grid_layout.addWidget(type, row, 0)     # 类型
            grid_layout.addWidget(ratio, row, 1)    # 结果
            grid_layout.addWidget(odds, row, 2)     # 赔率
            grid_layout.addWidget(source, row, 3)   # 来源
        self.setLayout(grid_layout)
        # self.setStyleSheet("padding: 0px;")
        self.setStyleSheet("""
            padding: 0px;   /* 设置控件的内边框距离 */
            background-color: transparent; /* 设置透明背景 */
            border: none; /* 去掉边框 */
        """)

    def mousePressEvent(self, event):
        """重写整个卡片点击事件, 发送携带数据的信号"""
        super().mousePressEvent(event)  # 调用父类的事件处理
        self.signal_odds.emit(self.__data)    # 发出信号，并携带数据
